<?php

/**
 *[JishiGou] (C)2005 - 2099 Cenwor Inc.
 *
 标签管理
 *
 * @author 狐狸<foxis@qq.com>
 * @package www.jishigou.net
 */
include_once(ROOT_PATH . 'include/function/misc.func.php');
if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject
{
	var $ID = 0;


	
	function ModuleObject($config)
	{
		$this->MasterObject($config);
		$this->ID = $this->Get['id']?(int)$this->Get['id']:(int)$this->Post['id'];
		$this->Execute();
	}

	
	function Execute()
	{
		ob_start();
		switch($this->Code)
		{
			case "delete":
				$this->delete();
				break;
				
			case "num_setting":
				$this->NumSetting();
				break;
				
			case "tag_status":
				$this->Tag_Status();
				break;
				
			default:
				$this->Main();
				break;
		}
		$body = ob_get_clean();
		
		$this->ShowBody($body);
		
	}
	
	
	function Main()
	{

		
				$keyword=trim($this->Get['keyword']);
		
		$where_list=array();
		if($keyword)$where_list[]="`name` like '%$keyword%'";
		$where="";
		$where = (empty($where_list)) ? null : ' WHERE '.implode(' AND ',$where_list).' ';
		
		
		$per_page_num=max((int)$this->Get['pn'],10);
		$offset=(max((int)$this->Get['page'],1)-1)*$per_page_num;
		
				$sql="SELECT count(1) total from ".TABLE_PREFIX."tag $where";
		$query = $this->DatabaseHandler->Query($sql);
		$row=$query->GetRow();
		$total=$row['total'];
		
				$order_by_list = array
		(
			'order_by_default' => 'default',
		
			'default' => array
			(
				'name' => '添加时间',
				'order_by' => 'id',
			),
			'thread_count' => array
			(
				'name' => '使用次数',
				'order_by' => 'total_count',
			),		
			'member_count' => array
			(
				'name' => '标签名称',
				'order_by' => 'name',
			),	
		);
		
		$order_condition = order
		(
			$order_by_list,
			"admin.php?mod=tag&keyword=".urlencode($keyword),
			array('display_un_href'=>true)
		);
		
		$sql="SELECT * 
		FROM ".TABLE_PREFIX."tag 
		$where 
		{$order_condition['order']} 
		limit $offset,$per_page_num";
		
		$query = $this->DatabaseHandler->Query($sql);
		$tag_list=array();
		while ($row=$query->GetRow()) 
		{
			
			$row['dateline']=my_date_format($row['dateline']);
			$row['tag_html'] = $this->_tag_html($row['id'],$row['status']);
			
			$tag_list[]=$row;
		}
					
	
		$pages=page($total,$per_page_num,'',array(),"10 20 30 50 100");
		
		$tag_item_list = ConfigHandler::get('tag','item_list');
		
		$tag_num = ConfigHandler::get('tag_num');
		foreach ($tag_item_list as $key=>$val)
		{
			$val["selected_" . (int) $tag_num[$val['value']]] = " selected ";
			
			$tag_item_list[$key] = $val;
			
		}
		
		include($this->TemplateHandler->Template('admin/tag'));
	}
	
	function delete()
	{
		$_tmp_arr = (array) ($this->Get['id'] ? $this->Get['id'] : $this->Post['delete']);
		$id_arr = array();
		foreach ($_tmp_arr as $_id) {
			$_id = (int) $_id;
			if($_id > 0) {
				$id_arr[$_id] = $_id;
			}
		}		
		if(!$id_arr) {
			$this->Messager("未指定删除的标签");
		}
		
		$sql = "SELECT * FROM `".TABLE_PREFIX."tag` WHERE `id` in('".implode("','",$id_arr)."')";
		$query = $this->DatabaseHandler->Query($sql);
		$tag_id_list = $tag_list = array();
		while ($row = $query->GetRow()) 
		{
			$id = (int) $row['id'];
			if ($id < 1) {
				continue ;
			}
			
			$tag_id_list[$id] = $id;
			$tag_list[$id] = $row['name'];			
		}
		if (!$tag_id_list) {
			$this->Messager("请指定删除的标签");
		}		
		$ids = "'".implode("','",$tag_id_list)."'";		
		
				$sql="DELETE FROM `".TABLE_PREFIX."tag` WHERE `id` IN($ids)";
		$this->DatabaseHandler->Query($sql,"SKIP_ERROR");
		
				$sql="DELETE FROM `".TABLE_PREFIX."my_tag` WHERE `tag_id` IN($ids)";
		$this->DatabaseHandler->Query($sql,"SKIP_ERROR");	
			
				$item_list=ConfigHandler::get('tag','item_list');
		foreach ($item_list as $item)
		{
			$sql="DELETE FROM `".$item['table_name']."_tag` WHERE `tag_id` IN($ids)";
			$this->DatabaseHandler->Query($sql,"SKIP_ERROR");
			
			$sql="DELETE FROM `".TABLE_PREFIX."my_{$item['value']}_tag` WHERE `tag_id` IN($ids)";
			$this->DatabaseHandler->Query($sql,"SKIP_ERROR");	

			foreach ($tag_list as $tag) {
				if (!$tag) {
					continue ;
				}
				
								$sql = "UPDATE `{$item['table_name']}` SET `tag`=TRIM(LEADING ',' FROM REPLACE(CONCAT_WS('',',',tag),',{$tag}','')) , `tag_count`=if(`tag_count`>1,`tag_count`-1,0) WHERE `tag` LIKE '%{$tag}%'";
				$this->DatabaseHandler->Query($sql,"SKIP_ERROR");
			}
		}
		
		clearcache();
		
		$this->Messager("删除成功");
	}

	function NumSetting()
	{		
		ConfigHandler::set('tag_num',$_POST['tag_num']);
		
		$this->Messager("设置成功",'admin.php?mod=tag');
	}
	
	function Tag_Status()
	{ 
		$this->DatabaseHandler->SetTable(TABLE_PREFIX.'tag');
		$tag_info = $this-> DatabaseHandler->Select($this->Get['tag_ids']);
		
		if($tag_info['status']){	
		  $tag_status = 0;
		}
		else{
			$tag_status = 1;
		}
				$uptag_sql = "update `".TABLE_PREFIX."tag` set `status`='{$tag_status}' where `id`=".$this->Get['tag_ids'];
		$this->DatabaseHandler->Query($uptag_sql);
		
		$this->Messager("设置成功",'admin.php?mod=tag');
	}
	
		function _tag_html($tag_id,$status=0,$addhtml=true) {
	
		$html = "";
			if ($status) {		
							$html = "<a href='admin.php?mod=tag&code=tag_status&tag_ids={$tag_id}'><font color='#008080'>已推荐,点此取消</font></a>";
			} else {
							$html = "<a href='admin.php?mod=tag&code=tag_status&tag_ids={$tag_id}'><font color='#804040'>未推荐,点此推荐</font></a>";
			}
			if($addhtml) $html = "<span id='tag_{$tag_id}'>{$html}</span>";
			
		return $html;
	}
	
}

?>
